package org.example.what_mall.config;

import lombok.RequiredArgsConstructor;
import org.example.what_mall.interceptor.AdminInterceptor;
import org.example.what_mall.interceptor.LoginInterceptor;
import org.example.what_mall.interceptor.OrdinaryInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@RequiredArgsConstructor
public class SpringMvcConfig implements WebMvcConfigurer {
    private final LoginInterceptor loginInterceptor;
    private final AdminInterceptor adminInterceptor;
    private final OrdinaryInterceptor ordinaryInterceptor;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .allowedHeaders("*")
                .exposedHeaders("token")
                .allowCredentials(true);
    }
    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        registry.addInterceptor(ordinaryInterceptor)
                .addPathPatterns("/api/**");

        registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/api/**")
                .excludePathPatterns("/api/user/login")
                .excludePathPatterns("/api/user/signup")
                .excludePathPatterns("/api/admin/login")
                .excludePathPatterns("/api/mall/getType")
                .excludePathPatterns("/api/mall/getGoodsByType")
                .excludePathPatterns("/api/mall/getGoodsInfo")
                .excludePathPatterns("/api/mall/searchGoods")
                .excludePathPatterns("/api/mall/getGoodsComment")
                .excludePathPatterns("/api/mall/askGoodsMsg");

        registry.addInterceptor(adminInterceptor)
                .addPathPatterns("/api/admin/**")
                .excludePathPatterns("/api/admin/login");


    }
}
